package org.openspaces.example.helloworld.feeder;

import java.awt.Point;

import org.openspaces.core.GigaSpace;
import org.openspaces.core.GigaSpaceConfigurer;
import org.openspaces.core.space.UrlSpaceConfigurer;
import org.openspaces.example.helloworld.common.Car;
import org.openspaces.example.helloworld.common.Direction;
import org.openspaces.example.helloworld.common.Roxel;
import org.openspaces.example.helloworld.common.StructureTuple;

import com.j_spaces.core.IJSpace;

public class CarFactory {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
        // connect to the space using its URL
  	    IJSpace space = new UrlSpaceConfigurer("jini://*/*/processorSpace").space();
        // use gigaspace wrapper to for simpler API
        GigaSpace gigaSpace = new GigaSpaceConfigurer(space).gigaSpace();
        
    	StructureTuple struc = new StructureTuple();
    	struc.setProcessed(true);
    	struc = gigaSpace.read(struc);
        
    	int set = 2;
    	Roxel template;
    	
    	switch(set){
    	case 0 :
        	template = new Roxel();
            template.setX(0);
            template.setY(3);
            template.setTileCoords(new Point(0,0));
            template = gigaSpace.take(template);
            if(template != null){
	            template.setCar(new Car(Direction.EAST));
	            template.setOccupied(true);
	            gigaSpace.write(template);
            }
            
            template = new Roxel();
            template.setX(1);
            template.setY(3);
            template.setTileCoords(new Point(0,0));
            template = gigaSpace.take(template);
            if(template != null){
	            template.setCar(new Car(Direction.EAST));
	            template.setOccupied(true);
	            gigaSpace.write(template);
            }
            template = new Roxel();
            template.setX(2);
            template.setY(3);
            template.setTileCoords(new Point(0,0));
            template = gigaSpace.take(template);
            if(template != null){
	            template.setCar(new Car(Direction.EAST));
	            template.setOccupied(true);
	            gigaSpace.write(template);
            }
            template = new Roxel();
            template.setX(3);
            template.setY(1);
            template.setTileCoords(new Point(0,0));
            template = gigaSpace.take(template);
            if(template != null){
	            template.setCar(new Car(Direction.SOUTH));
	            template.setOccupied(true);
	            gigaSpace.write(template);
            }
	        break;
    	case 1 :
    		for(int i = 0; i < struc.getBlocksX(); i++){
    			for( int j = 0; j < struc.getBlocksY(); j++){
    				if(j == 0){
    		            template = new Roxel();
    		            template.setX(3);
    		            template.setY(0);
    		            template.setTileCoords(new Point(i,j));
    		            template = gigaSpace.take(template);	
    		            if(template != null){
	    		            template.setCar(new Car(Direction.SOUTH));
	    		            template.setOccupied(true);
	    		            gigaSpace.write(template);
    		            }
    				}
    				if(i == 0){
    		            template = new Roxel();
    		            template.setX(0);
    		            template.setY(3);
    		            template.setTileCoords(new Point(i,j));
    		            template = gigaSpace.take(template);
    		            if(template != null){
	    		            template.setCar(new Car(Direction.EAST));
	    		            template.setOccupied(true);
	    		            gigaSpace.write(template);
    		            }
    				}
    			}
    		}
    		break;
    	case 2 :
    		for(int i = 0; i < struc.getBlocksX(); i++){
    			for( int j = 0; j < struc.getBlocksY(); j++){
    				if(j == 0){
    					for(int c = 0; c < struc.getBlockLen()/2;c++){
        		            template = new Roxel();
        		            template.setX(3);
        		            template.setY(c);
        		            template.setTileCoords(new Point(i,j));
        		            template = gigaSpace.take(template);
        		            if(template != null){
	        		            template.setCar(new Car(Direction.SOUTH));
	        		            template.setOccupied(true);
	        		            gigaSpace.write(template);
    					
        		            }
    					}
    				}
    				if(i == 0){
    					for(int c = 0; c < struc.getBlockLen()/2;c++){
	    		            template = new Roxel();
	    		            template.setX(c);
	    		            template.setY(3);
	    		            template.setTileCoords(new Point(i,j));
	    		            template = gigaSpace.take(template);
	    		            if(template != null){
		    		            template.setCar(new Car(Direction.EAST));
		    		            template.setOccupied(true);
		    		            gigaSpace.write(template);
	    		            }
    					}
    				}
    			}
    		}
    		break;
    	}

        

	}

}
